package com.sanhai.android.view.dialog;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.webkit.WebView;
import android.widget.ImageView;

import com.sanhai.android.R;

import java.util.Timer;
import java.util.TimerTask;


public class ProgressDialog extends BaseDialog {
    private int[] imgIds = new int[]{R.drawable.one, R.drawable.two, R.drawable.three, R.drawable.four, R.drawable.five, R.drawable.six, R.drawable.seven, R.drawable.eight, R.drawable.nine, R.drawable.ten, R.drawable.eleven, R.drawable.twelve};
    private ImageView mIv;
    private int tag = 1;

    private Timer timer = null;
    private TimerTask task = null;
    private WebView web;

    public ProgressDialog(Context context) {
        super(context);
        setContentView(R.layout.dialog_progress);
        mIv = (ImageView) findViewById(R.id.imgae);
        startTime();

    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) mIv.getBackground();
            if (bitmapDrawable != null) {
                Bitmap hisBitmap = bitmapDrawable.getBitmap();
                if (hisBitmap.isRecycled() == false) {
                    hisBitmap.recycle();
                }
            }
            mIv.setBackgroundResource(imgIds[msg.arg1]);
            startTime();

        }

        ;
    };

    public void startTime() {
        if (timer == null) {
            timer = new Timer();
        }

        task = new TimerTask() {
            @Override
            public void run() {
                tag++;
                Message message = mHandler.obtainMessage();
                message.arg1 = tag;
                mHandler.sendMessage(message);
                if (tag == 11) {
                    tag = 0;
                }
            }
        };

        timer.schedule(task, 100);
    }

}
